package com.commpany.servie.basic.product.service.read;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.ArrayList;
import com.commpany.servie.basic.product.api.read.ProductReadService;
import com.commpany.jdq.core.common.PageResult;
import com.commpany.jdq.core.orm.Pagination;
import com.commpany.servie.basic.product.business.read.product.ProductReadManage;
import com.commpany.servie.basic.product.model.dto.product.ProductDTO;
import com.commpany.servie.basic.product.converter.ProductConverter;
import com.commpany.servie.basic.product.po.ProductPO;
import com.commpany.servie.basic.product.model.dto.PageResultDTO;
@Service("productReadService")
public class ProductReadServiceImpl  implements ProductReadService {
	@Autowired
	private ProductReadManage productReadManage;
	
	public ProductDTO getProductService(ProductDTO inputDTO){


	   return ProductConverter.toDTO(productReadManage.getProductService(ProductConverter.toPO(inputDTO)));
	}
   @Override
	public PageResultDTO<ProductDTO> selectProductPageService(ProductDTO inputDTO){

		ProductPO po = ProductConverter.toPO(inputDTO);
		Pagination page = new Pagination();
	    page.setLimitStart(inputDTO.getLimitStart());
		page.setPageNo(inputDTO.getPageNo());
		page.setPageSize(inputDTO.getPageSize());
		Optional.ofNullable(inputDTO.getOrderBy()).ifPresent(page::setOrderBy);
		PageResult<ProductPO> poPage = productReadManage.selectProductPageService(po,page);
		List<ProductPO> poList = poPage.getList();
		if(Objects.isNull(poList)) {
	    	 return PageResultDTO.<ProductDTO>builder().build();
	     }		
		 List<ProductDTO> dtoList = poList.stream().map(src-> ProductConverter.toDTO(src)).collect(Collectors.toList());
		return 	PageResultDTO.<ProductDTO>builder().pageNo(poPage.getPageNo()).pageSize(poPage.getPageSize()).list(dtoList).build();
	}
   @Override
    public int countProductService( ProductDTO inputDTO ){
         ProductPO po = ProductConverter.toPO(inputDTO);
         return productReadManage.countProductService(po);
    }
     @Override
	public List<ProductDTO> queryProductService (ProductDTO inputDTO){
	    ProductPO po = ProductConverter.toPO(inputDTO);
	     List<ProductPO> list = productReadManage.queryProductService(po);
		   if(Objects.isNull(list)) {
	    	 return new ArrayList<ProductDTO>();
	     }
		return list.stream().map(src-> ProductConverter.toDTO(src)).collect(Collectors.toList());
	}
}
	